package com.example.ecas.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;


@JsonInclude(JsonInclude.Include.NON_NULL)//序列化的时候会把空值忽略
public class  CommonResponse<T> {
    private final int code;
    private  String msg;
    private  T data;//泛型


    private  CommonResponse(int status, String msg){
        this.code =status;
        this.msg=msg;
    }//只传数据
    private CommonResponse(int status, T data){
        this.data=data;
        this.code =status;
    }
    private CommonResponse(int status, String msg, T data){
        this.code =status;
        this.msg=msg;
        this.data=data;
    }

    @JsonIgnore
    public boolean isSuccess(){
        return this.code == com.example.ecas.common.ResponseCode.SUCCESS.getCode();
    }

    public static <T>CommonResponse<T> creatForSuccess(){
        return new CommonResponse<>(com.example.ecas.common.ResponseCode.SUCCESS.getCode(), com.example.ecas.common.ResponseCode.SUCCESS.getDesc());
    }
    public static <T>CommonResponse<T> creatForSuccessMessage(String msg){
        return new CommonResponse<>(com.example.ecas.common.ResponseCode.SUCCESS.getCode(), msg);
    }
    public static <T>CommonResponse<T> creatForSuccess(String msg, T data){
        return new CommonResponse<>(com.example.ecas.common.ResponseCode.SUCCESS.getCode(), msg,data);
    }
    public static <T>CommonResponse<T> creatForSuccess(T data){
        return new CommonResponse<>(com.example.ecas.common.ResponseCode.SUCCESS.getCode(), com.example.ecas.common.ResponseCode.SUCCESS.getDesc(),data);
    }
    public static <T>CommonResponse<T> creatForError(String msg){
        return new CommonResponse<>(com.example.ecas.common.ResponseCode.ERROR.getCode(), msg);
    }
    public static <T>CommonResponse<T> creatForError(int code, String  msg){
        return new CommonResponse<>(code, msg);
    }
    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
